
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/special_topics/finite_diff_derivatives.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:25:56 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Finite Difference Approximations to Derivatives &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="finite_diff_derivatives.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Classification of SymPy objects" href="classification.html" />
    <link rel="prev" title="Introduction" href="intro.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="classification.html" title="Classification of SymPy objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="Introduction"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Special Topics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Finite Difference Approximations to Derivatives</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="finite-difference-approximations-to-derivatives">
<h1>Finite Difference Approximations to Derivatives<a class="headerlink" href="#finite-difference-approximations-to-derivatives" title="Permalink to this headline">¶</a></h1>
<section id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>Finite difference approximations to derivatives is quite important in numerical analysis and in
computational physics. In this tutorial we show how to use SymPy to compute  approximations of
varying accuracy. The hope is that these notes could be useful for the practicing researcher who
is developing code in some language and needs to be able to efficiently generate finite difference
formulae for various approximations.</p>
<p>In order to establish notation, we first state that we envision that there exists a continuous function F of a single
variable x, with F having as many derivatives as desired.  We sample x values uniformly at points along the
real line separated by h.  In most cases we want h to be small in some sense.  F(x) may be expanded
about some point <span class="math notranslate nohighlight">\(x_{0}\)</span> via the usual Taylor series expansion. Let <span class="math notranslate nohighlight">\(x = x_{0} + h\)</span>. Then the Taylor expansion is</p>
<div class="math notranslate nohighlight">
\[F(x_{0}+h) = F(x_{0}) + \big(\frac{dF}{dx}\big)_{x_{0}} * h +  \frac{1}{2!} \big(\frac{d^{2}F }{dx^{2}}\big)_{x_{0}}* h^2 +
\frac{1}{3!} \big(\frac{d^{3}F }{dx^{3}}\big)_{x_{0}}* h^3 + ...\]</div>
<p>In order to simplify the notation, we now define a set of coefficients <span class="math notranslate nohighlight">\(c_{n}\)</span>, where</p>
<div class="math notranslate nohighlight">
\[c_{n} := \frac{1}{n!} \big(\frac{d^{n}F }{dx^{n}}\big)_{x_{0}}.\]</div>
<p>So now our series has the form:</p>
<div class="math notranslate nohighlight">
\[F(x_{0}+h) = F(x_{0}) + c_{1} * h +  c_{2}* h^2 + c_{3}* h^3 + ...\]</div>
<p>In the following we will only use a finite grid of values <span class="math notranslate nohighlight">\(x_{i}\)</span> with <span class="math notranslate nohighlight">\(i\)</span> running from <span class="math notranslate nohighlight">\(1,...,N\)</span> and the corresponding values of our function
F at these grid points denoted by <span class="math notranslate nohighlight">\(F_{i}\)</span>.  So the problem is how to generate approximate values for the derivatives of F with the constraint that
we use a subset of the finite set of pairs <span class="math notranslate nohighlight">\((x_{i},F_{i})\)</span> of size N.</p>
<p>What follows are  manipulations using SymPy to formulate approximations for derivatives of a given order and to assess its accuracy.
First, we use SymPy to derive the approximations by using a rather brute force method frequently covered in introductory treatments. Later we shall make use of other SymPy functions which get the job done with more efficiency.</p>
</section>
<section id="a-direct-method-using-sympy-matrices">
<h2>A Direct Method Using SymPy Matrices<a class="headerlink" href="#a-direct-method-using-sympy-matrices" title="Permalink to this headline">¶</a></h2>
<p>If we let <span class="math notranslate nohighlight">\(x_{0} = x_{i}\)</span>, evaluate the series at <span class="math notranslate nohighlight">\(x_{i+1}=x_{i}+ h\)</span> and truncate all terms above <span class="math notranslate nohighlight">\(O(h^1)\)</span> we can solve for the single coefficient <span class="math notranslate nohighlight">\(c_{1}\)</span> and obtain an approximation to the first derivative:</p>
<div class="math notranslate nohighlight">
\[\big(\frac{dF}{dx}\big)_{x_{0}} \approx \frac{F_{i+1} - F_{i}}{h} + O(h)\]</div>
<p>where the <span class="math notranslate nohighlight">\(O(h)\)</span> refers to the lowest order term in the series in <span class="math notranslate nohighlight">\(h\)</span>.  This establishes that the derivative
approximation is of first order accuracy.  Put another way, if we decide that we can only use the two pairs
<span class="math notranslate nohighlight">\((x_{i},F_{i})\)</span> and <span class="math notranslate nohighlight">\((x_{i+1},F_{i+1})\)</span> we obtain a “first order accurate” derivative.</p>
<p>In addition to <span class="math notranslate nohighlight">\((x_{i},F_{i})\)</span> we next use the two points <span class="math notranslate nohighlight">\((x_{i+1},F_{i+1})\)</span> and <span class="math notranslate nohighlight">\((x_{i+2},F_{i+2})\)</span>.
Then we have two equations:</p>
<div class="math notranslate nohighlight">
\[F_{i+1} = F_{i} + c_{1}* h + \frac{1}{2}*c_{2}*h^2 + \frac{1}{3!}*c_{3}*h^3 + ...\]</div>
<div class="math notranslate nohighlight">
\[F_{i+2} = F_{i} + c_{1}* (2h) + \frac{1}{2}*c_{2}*(2h)^2 + \frac{1}{3!}*c_{3}*(2h)^3 + ...\]</div>
<p>If we again want to find the first derivative (<span class="math notranslate nohighlight">\(c_{1}\)</span>), we can do that by eliminating the term involving <span class="math notranslate nohighlight">\(c_{2}\)</span> from
the two equations.  We show how to do it using SymPy.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x, x_0, h&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Fi</span><span class="p">,</span> <span class="n">Fip1</span><span class="p">,</span> <span class="n">Fip2</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;F_</span><span class="si">{i}</span><span class="s1">, F_{i+1}, F_{i+2}&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># there are the coefficients c_0=Fi, c_1=dF/dx, c_2=d**2F/dx**2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;c:3&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">P</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span> <span class="p">((</span><span class="mi">1</span><span class="o">/</span><span class="n">factorial</span><span class="p">(</span><span class="n">i</span><span class="p">))</span><span class="o">*</span><span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">x0</span><span class="p">)</span><span class="o">**</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="p">)</span>
</pre></div>
</div>
<p>Vector of right hand sides:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">R</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">Fi</span><span class="p">],</span> <span class="p">[</span><span class="n">Fip1</span><span class="p">],</span> <span class="p">[</span><span class="n">Fip2</span><span class="p">]])</span>
</pre></div>
</div>
<p>Now we make a matrix consisting of the coefficients
of the c_i in the nth degree polynomial P.</p>
<p>Coefficients of <span class="math notranslate nohighlight">\(c_i\)</span> evaluated at <span class="math notranslate nohighlight">\(x_i\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m11</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span> <span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m12</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span> <span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m13</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span> <span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
<p>Coefficients of <span class="math notranslate nohighlight">\(c_i\)</span> evaluated at <span class="math notranslate nohighlight">\(x_i + h\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m21</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m22</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m23</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
<p>Coefficients of <span class="math notranslate nohighlight">\(c_i\)</span> evaluated at <span class="math notranslate nohighlight">\(x_i + 2*h\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m31</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m32</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m33</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
<p>Matrix of the coefficients is 3x3 in this case:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">m11</span><span class="p">,</span> <span class="n">m12</span><span class="p">,</span> <span class="n">m13</span><span class="p">],</span> <span class="p">[</span><span class="n">m21</span><span class="p">,</span> <span class="n">m22</span><span class="p">,</span> <span class="n">m23</span><span class="p">],</span> <span class="p">[</span><span class="n">m31</span><span class="p">,</span> <span class="n">m32</span><span class="p">,</span> <span class="n">m33</span><span class="p">]])</span>
</pre></div>
</div>
<p>Matrix form of the three equations for the <span class="math notranslate nohighlight">\(c_i\)</span> is M*X = R:</p>
<p>The solution is obtained by directly inverting the 3x3 matrix M:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span>  <span class="n">M</span><span class="o">.</span><span class="n">inv</span><span class="p">()</span> <span class="o">*</span> <span class="n">R</span>
</pre></div>
</div>
<p>Note that all three coefficients make up the solution. The desired first derivative is coefficient <span class="math notranslate nohighlight">\(c_1\)</span> which is X[1].</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">together</span><span class="p">(</span><span class="n">X</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="go">(4*F_{i+1} - F_{i+2} - 3*F_{i})/(2*h)</span>
</pre></div>
</div>
<p>It is instructive to compute another three-point approximation to the first derivative,  except centering the approximation
at <span class="math notranslate nohighlight">\(x_i\)</span> and thus using points at <span class="math notranslate nohighlight">\(x_{i-1}\)</span>,  <span class="math notranslate nohighlight">\(x_{i}\)</span>,  and <span class="math notranslate nohighlight">\(x_{i+1}\)</span>. So here is how this can be done using the ‘brute force’ method:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x, x_i, h&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Fi</span><span class="p">,</span> <span class="n">Fim1</span><span class="p">,</span> <span class="n">Fip1</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;F_</span><span class="si">{i}</span><span class="s1">, F_{i-1}, F_{i+1}&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># there are the coefficients c_0=Fi,  c_1=dF/h,  c_2=d**2F/h**2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;c:3&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># define a polynomial of degree n</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">P</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="gp">... </span>   <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span> <span class="p">((</span><span class="mi">1</span><span class="o">/</span><span class="n">factorial</span><span class="p">(</span><span class="n">i</span><span class="p">))</span><span class="o">*</span><span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">x0</span><span class="p">)</span><span class="o">**</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># now we make a matrix consisting of the coefficients</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># of the c_i in the nth degree polynomial P</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># coefficients of c_i evaluated at x_i</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m11</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span> <span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m12</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span> <span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m13</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span> <span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># coefficients of c_i evaluated at x_i - h</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m21</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m22</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m23</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># coefficients of c_i evaluated at x_i + h</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m31</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m32</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m33</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># matrix of the coefficients is 3x3 in this case</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">m11</span><span class="p">,</span> <span class="n">m12</span><span class="p">,</span> <span class="n">m13</span><span class="p">],</span> <span class="p">[</span><span class="n">m21</span><span class="p">,</span> <span class="n">m22</span><span class="p">,</span> <span class="n">m23</span><span class="p">],</span> <span class="p">[</span><span class="n">m31</span><span class="p">,</span> <span class="n">m32</span><span class="p">,</span> <span class="n">m33</span><span class="p">]])</span>
</pre></div>
</div>
<p>Now that we have the matrix of coefficients we next form the right-hand-side and solve by inverting <span class="math notranslate nohighlight">\(M\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># matrix of the function values...actually a vector of right hand sides</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">R</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">Fi</span><span class="p">],</span> <span class="p">[</span><span class="n">Fim1</span><span class="p">],</span> <span class="p">[</span><span class="n">Fip1</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># matrix form of the three equations for the c_i is M*X = R</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># solution directly inverting the 3x3 matrix M:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span>  <span class="n">M</span><span class="o">.</span><span class="n">inv</span><span class="p">()</span> <span class="o">*</span> <span class="n">R</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># note that all three coefficients make up the solution</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># the first derivative is coefficient c_1 which is X[1].</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;The second-order accurate approximation for the first derivative is: &quot;</span><span class="p">)</span>
<span class="go">The second-order accurate approximation for the first derivative is:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">together</span><span class="p">(</span><span class="n">X</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="go">(F_{i+1} - F_{i-1})/(2*h)</span>
</pre></div>
</div>
<p>These two examples serve to show how one can directly find second order accurate first derivatives using SymPy.
The first example uses values of <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(F\)</span> at all three points <span class="math notranslate nohighlight">\(x_i\)</span>, <span class="math notranslate nohighlight">\(x_{i+1}\)</span>, and <span class="math notranslate nohighlight">\(x_{i+2}\)</span> whereas the
second example only uses values of <span class="math notranslate nohighlight">\(x\)</span> at the two points <span class="math notranslate nohighlight">\(x_{i-1}\)</span> and <span class="math notranslate nohighlight">\(x_{i+1}\)</span> and thus is a bit more efficient.</p>
<p>From these two simple examples a general rule is that if one wants a first derivative to be accurate to <span class="math notranslate nohighlight">\(O(h^{n})\)</span>
then one needs n+1 function values in the approximating polynomial (here provided via the function <span class="math notranslate nohighlight">\(P(x,x0,c,n)\)</span>).</p>
<p>Now let’s assess the question of the accuracy of the centered difference result to see how we determine that it is
really second order.  To do this we take the result for <span class="math notranslate nohighlight">\(dF/dx\)</span> and substitute in the polynomial expansion for a higher
order polynomial and see what we get. To this end,  we make a set of eight coefficients d and use them to perform the
check:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;c:8&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dfdxcheck</span> <span class="o">=</span> <span class="p">(</span><span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span> <span class="o">-</span> <span class="n">P</span><span class="p">(</span><span class="n">x0</span><span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">simplify</span><span class="p">(</span><span class="n">dfdxcheck</span><span class="p">))</span> <span class="c1"># so the appropriate cancellation of terms involving `h` happens</span>
<span class="go">c1 + c3*h**2/6 + c5*h**4/120 + c7*h**6/5040</span>
</pre></div>
</div>
<p>Thus we see that indeed the derivative is <span class="math notranslate nohighlight">\(c_1\)</span> with the next term in the series of order <span class="math notranslate nohighlight">\(h^2\)</span>.</p>
<p>However,  it can quickly become rather tedious to generalize the direct method as presented above when attempting
to generate a derivative approximation to high order,  such as 6 or 8 although the method certainly works and using
the present method is certainly less tedious than performing the calculations by hand.</p>
<p>As we have seen in the discussion above,  the simple centered approximation for the first derivative only uses two
point values of the <span class="math notranslate nohighlight">\((x_{i},F_{i})\)</span> pairs.  This works fine until one encounters the last point in the domain,  say at
<span class="math notranslate nohighlight">\(i=N\)</span>. Since our centered derivative approximation would use data at the point <span class="math notranslate nohighlight">\((x_{N+1},F_{N+1})\)</span> we see that the
derivative formula will not work. So,  what to do?  Well,  a simple way to handle this is to devise a different formula
for this last point which uses points for which we do have values. This is the so-called backward difference formula.
To obtain it,  we can use the same direct approach,  except now us the three points <span class="math notranslate nohighlight">\((x_{N},F_{N})\)</span>,  <span class="math notranslate nohighlight">\((x_{N-1},F_{N-1})\)</span>,
and <span class="math notranslate nohighlight">\((x_{N-2},F_{N-2})\)</span> and center the approximation at <span class="math notranslate nohighlight">\((x_{N},F_{N})\)</span>. Here is how it can be done using SymPy:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x, x_N, h&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FN</span><span class="p">,</span> <span class="n">FNm1</span><span class="p">,</span> <span class="n">FNm2</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;F_</span><span class="si">{N}</span><span class="s1">, F_{N-1}, F_{N-2}&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">8</span> <span class="c1"># there are the coefficients c_0=Fi,  c_1=dF/h,  c_2=d**2F/h**2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;c:8&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># define a polynomial of degree d</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">P</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span> <span class="p">((</span><span class="mi">1</span><span class="o">/</span><span class="n">factorial</span><span class="p">(</span><span class="n">i</span><span class="p">))</span><span class="o">*</span><span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">x0</span><span class="p">)</span><span class="o">**</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="p">)</span>
</pre></div>
</div>
<p>Now we make a matrix consisting of the coefficients of the <span class="math notranslate nohighlight">\(c_i\)</span> in the dth
degree polynomial P coefficients of <span class="math notranslate nohighlight">\(c_i\)</span> evaluated at <span class="math notranslate nohighlight">\(x_i, x_{i-1},\)</span> and <span class="math notranslate nohighlight">\(x_{i+1}\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m11</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span> <span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m12</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m13</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span> <span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># coefficients of c_i evaluated at x_i - h</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m21</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span><span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m22</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span><span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m23</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span><span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># coefficients of c_i evaluated at x_i + h</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m31</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m32</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m33</span> <span class="o">=</span> <span class="n">P</span><span class="p">(</span><span class="n">xN</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">,</span> <span class="n">xN</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
<p>Next we construct the <span class="math notranslate nohighlight">\(3 \times 3\)</span> matrix of the coefficients:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">m11</span><span class="p">,</span> <span class="n">m12</span><span class="p">,</span> <span class="n">m13</span><span class="p">],</span> <span class="p">[</span><span class="n">m21</span><span class="p">,</span> <span class="n">m22</span><span class="p">,</span> <span class="n">m23</span><span class="p">],</span> <span class="p">[</span><span class="n">m31</span><span class="p">,</span> <span class="n">m32</span><span class="p">,</span> <span class="n">m33</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># matrix of the function values...actually a vector of right hand sides</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">R</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">FN</span><span class="p">],</span> <span class="p">[</span><span class="n">FNm1</span><span class="p">],</span> <span class="p">[</span><span class="n">FNm2</span><span class="p">]])</span>
</pre></div>
</div>
<p>Then we invert <span class="math notranslate nohighlight">\(M\)</span> and write the solution to the <span class="math notranslate nohighlight">\(3 \times 3\)</span> system.</p>
<p>The matrix form of the three equations for the c_i is <span class="math notranslate nohighlight">\(M*C = R\)</span>. The solution is obtained by
directly inverting <span class="math notranslate nohighlight">\(M\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span>  <span class="n">M</span><span class="o">.</span><span class="n">inv</span><span class="p">()</span> <span class="o">*</span> <span class="n">R</span>
</pre></div>
</div>
<p>The first derivative is coefficient <span class="math notranslate nohighlight">\(c_1\)</span> which is <span class="math notranslate nohighlight">\(X[1]\)</span>. Thus the second order accurate
approximation for the first derivative is:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;The first derivative centered at the last point on the right is:&quot;</span><span class="p">)</span>
<span class="go">The first derivative centered at the last point on the right is:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">together</span><span class="p">(</span><span class="n">X</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="go">(-4*F_{N-1} + F_{N-2} + 3*F_{N})/(2*h)</span>
</pre></div>
</div>
<p>Of course,  we can devise a similar formula for the value of the derivative at the left end
of the set of points at <span class="math notranslate nohighlight">\((x_{1},F_{1})\)</span> in terms of values at <span class="math notranslate nohighlight">\((x_{2},F_{2})\)</span> and <span class="math notranslate nohighlight">\((x_{3},F_{3})\)</span>.</p>
<p>Also,  we note that output of formats appropriate to Fortran,  C,  etc. may be done in the examples
given above.</p>
<p>Next we show how to perform these and many other discritizations of derivatives,  but using a
much more efficient approach originally due to Bengt Fornberg and now incorporated into SymPy.</p>
<p><a class="reference internal" href="../tutorial/calculus.html#calculus-finite-differences"><span class="std std-ref">Finite differences</span></a></p>
<p><a class="reference internal" href="../modules/calculus/index.html#finite-diff"><span class="std std-ref">Finite difference weights</span></a></p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Finite Difference Approximations to Derivatives</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#a-direct-method-using-sympy-matrices">A Direct Method Using SymPy Matrices</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="intro.html"
                        title="previous chapter">Introduction</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="classification.html"
                        title="next chapter">Classification of SymPy objects</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/special_topics/finite_diff_derivatives.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="classification.html" title="Classification of SymPy objects"
             >next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="Introduction"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Special Topics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Finite Difference Approximations to Derivatives</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/special_topics/finite_diff_derivatives.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:25:56 GMT -->
</html>